import csv
import re


def str_len(s):
    # length of chinese chars
    return int((len(s.encode('utf-8')) - len(s)) / 2 + len(s))


def printTable(myDict, colList=None):

    if not colList:
        colList = list(myDict[0].keys() if myDict else [])
    myList = [colList]  # 1st row = header
    for item in myDict:
        myList.append([str(item[col] if item[col] is not None else '') for col in colList])

    colSize = [max(map(str_len, col)) for col in zip(*myList)]

    formatStr = ' | '.join(["{:}" for _ in colSize]) + '\n'

    myList.insert(1, ['-' * i for i in colSize])  # Seperating line
    s = ''
    # .format() doesn't work with non-english chars
    for i in range(len(myList)):
        s += formatStr.format(*list(map(lambda _s, l: ' ' * (l - str_len(_s)) + _s, myList[i], colSize)))
    return s


def dash(s):
    return s if s.strip() == '-' else s + ' %'


# split concrete blocks by line and merge the lines which are meant to be one
def split_strips(block):
    lines = block.split('\n')
    # print(lines)
    res = []
    concrete_line = 0  # = distance from current line to last concrete line
    tmp = ''
    for i in range(len(lines)):
        # strip() is needed; it'd be 6 otherwise
        if len(re.compile('\s+').split(lines[i].strip())) != 5:
            tmp = tmp + lines[i] + '\n'
            # print(0,tmp,concrete_line,res)
        else:
            if tmp != '' and concrete_line >= 1:  # last line is concrete, commit it
                res.append(tmp.strip())
                tmp = lines[i] + '\n'
                concrete_line = 0
                # print(1,tmp, concrete_line, res)
            elif concrete_line == 0:
                tmp = lines[i] + '\n'
                # print(2,tmp, concrete_line, res)
        concrete_line += 1
    if concrete_line >0:
        res.append(tmp)
        # print(3,tmp, concrete_line, res)

    return res


def run(path):
    with open(path, newline='', encoding="utf-8-sig", errors="ignore")as f:
        reader = csv.DictReader(f)
        triplet_w_code = {}
        for row in reader:
            code, content, flag = row["code"], row["content"], row["flag"]
            # if code!='000586':
            #     continue
            if content=='EMPTY':
                triplet_w_code[code]=[content,content,content]
                continue
            s = content

            try:

                t = re.search('【2\..+?】[^【]*?(【截止日期】[\s\S]+)【3\.', s)
                if t is None:
                    t = re.search('【2\..+?】[^【]*?(【截止日期】[\s\S]+)$', s)
                s = t.group(1)

                date = re.findall('【截止日期】.*?([\d]{4}-[\d]{2}-[\d]{2})', s)

                blocks = s.split('─────────────────────────────────────')

                res = []
                # zz=0
                for i in blocks:
                    # zz+=1
                    # if zz!=3:
                    #     continue
                    # print('===============================↓')
                    # print(i)
                    # print('===============================↑')

                    if '【截止日期】' not in i:
                        # print(i.split('\n'))
                        i = i.strip()
                        # print(i)
                        # print('------%--------%---%----------->')
                        tmp = split_strips(i)
                        # print(tmp)
                        # print('------%--------%---%----------->')
                        for j in tmp:
                            tags = {'(行': '(行业)', '业)': '(行业)',
                                    '(产': '(产品)', '品)': '(产品)',
                                    '(地': '(地区)', '区)': '(地区)'}
                            for z in tags:
                                if z in j:
                                    tag = tags[z]
                                    # print(j)
                                    j = re.compile('\s+').split(j.split('\n')[0])
                                    # print(j)
                                    if tag in j[0]:
                                        j[0] = j[0].replace(tag, '')
                                    if z in j[0]:
                                        j[0] = j[0].replace(z, '')
                                    header = j[0] + tag
                                    # header, last 2 percentage numbers
                                    # print(tmp)
                                    res.append((header, j[3], j[4]))

                    else:
                        res.append(date.pop(0))
                # print(res)
                toprint = []
                toprint_plan_b = []
                ss = []
                date_count = 0
                date_header = ''
                for index, i in enumerate(res):
                    # print(i)

                    if isinstance(i, str):
                        date_header = i

                        date_count += 1

                        if date_count > 1:
                            tp = printTable(toprint)
                            if tp.strip() == '':
                                ss.append(printTable(toprint_plan_b))
                            else:
                                ss.append(tp)
                            toprint = []
                            toprint_plan_b = []
                        if date_count > 3:
                            break
                    elif (not i[0].startswith('合计')) and ('(产品)' in i[0]):
                        toprint.append({date_header: i[0], '毛利': dash(i[1]), '占营收': dash(i[2])})
                        # print(toprint)
                    elif (not i[0].startswith('合计')) and ('(行业)' in i[0]):
                        toprint_plan_b.append({date_header: i[0], '毛利': dash(i[1]), '占营收': dash(i[2])})
                        # print(toprint_plan_b)
                    if index == len(res) - 1:
                        tp = printTable(toprint)
                        if tp.strip() == '':
                            ss.append(printTable(toprint_plan_b))
                        else:
                            ss.append(tp)
                triplet_w_code[code] = ss
            except Exception as e:
                print('Error occurred when processing f11 data')
                print(code, e)

        return triplet_w_code


if __name__ == "__main__":
    ss = run('res_2020-07-02.csv')
    for s in ss:
        if s=='000586':
            for i in ss[s]:
                print(i)
    # from common import bin_storage, file_mgmt
    # from datetime import datetime
    #
    # u = file_mgmt.push(dir="/home/ubuntu/f10_serverside/f11/res")
    # if u[1]:
    #     triplet_w_code = run(u[1])
    #     bin = bin_storage.Bin_storage()
    #     save_bin_name = '/home/ubuntu/f10_serverside/f11/bin/' + datetime.today().strftime('%Y-%m-%d')
    #     bin.save(save_bin_name, triplet_w_code)
    #     file_mgmt.push(dir="/home/ubuntu/f10_serverside/f11/bin")
